<?php
/**
 * Author: lf
 * Blog: https://blog.szdma.com
 * Email: job@szdma.com
 * Created at: 2016-03-21 18:45.
 */

namespace backend\grid;

use kartik\grid\GridView as kartikGridView;
use yii;
use yii\grid\GridViewAsset;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\widgets\BaseListView;
use yii\widgets\LinkPager;

/**
 * {@inheritdoc}
 */
class GridView extends kartikGridView
{
    /* @var $dataColumnClass \backend\grid\DataColumn */
    public $dataColumnClass = 'backend\grid\DataColumn';

    public $options = ['class' => 'fixed-table-header', 'style' => 'margin-right: 0px;'];

    public $tableOptions = ['class' => 'table table-hover'];

    // public $layout = "{items}\n<div class='row'><div class='col-sm-2' >{summary}</div><div class='col-sm-10'><div
    // class='dataTables_paginate paging_simple_numbers'>{pager}</div></div></div>";

    public $pagerOptions = [
        'firstPageLabel' => '首页',
        'lastPageLabel'  => '尾页',
        'prevPageLabel'  => '上一页',
        'nextPageLabel'  => '下一页',
        'options'        => [
            'class' => 'pagination',
        ],
    ];

    public $filterRow;

    /**
     * {@inheritdoc}
     */
    public function init()
    {
        parent::init();

        $this->rowOptions   = function ($model, $key, $index, $grid) {
            if (0 === $index % 2) {
                return ['class' => 'odd'];
            } else {
                return ['class' => 'even'];
            }
        };
        $this->pagerOptions = [
            'firstPageLabel' => yii::t('app', 'first'),
            'lastPageLabel'  => yii::t('app', 'last'),
            'prevPageLabel'  => yii::t('app', 'previous'),
            'nextPageLabel'  => yii::t('app', 'next'),
            'options'        => [
                'class' => 'pagination',
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function renderTableRow($model, $key, $index)
    {
        if (null !== $this->filterRow && false === call_user_func($this->filterRow, $model, $key, $index, $this)) {
            return '';
        }

        return parent::renderTableRow($model, $key, $index);
    }

    /**
     * {@inheritdoc}
     */
    public function renderPager()
    {
        $pagination = $this->dataProvider->getPagination();
        if (false === $pagination || $this->dataProvider->getCount() <= 0) {
            return '';
        }
        /* @var $class LinkPager */
        $pager               = $this->pager;
        $class               = ArrayHelper::remove($pager, 'class', LinkPager::className());
        $pager['pagination'] = $pagination;
        $pager['view']       = $this->getView();
        $pager               = array_merge($pager, $this->pagerOptions);

        return $class::widget($pager);
    }

    /**
     * {@inheritdoc}
     */
    public function run()
    {
        $id      = $this->options['id'];
        $options = Json::htmlEncode($this->getClientOptions());
        $view    = $this->getView();
        GridViewAsset::register($view);
        $view->registerJs("jQuery('#$id').yiiGridView($options);");
        BaseListView::run();
    }
}
